Welcome Guest | Sign in | Register

Home > Java Programming > Objects and Collections > Questions and Answers

Exercise:

Section 1

01. What will be the output of the program?
public class Test
{
public static void main (String args[])
{
String str = NULL;
System.out.println(str);
}
}
A. NULL B. Compile Error
C. Code runs but no output D. Runtime Exception

Answer and Explanation

Answer: Compile Error

Explanation:
Option B is correct because to set the value of a String variable to null you must use "null" and not "NULL".

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What will be the output of the program?
public class Test
{
private static int[] x;
public static void main(String[] args)
{
System.out.println(x[0]);
}
}
A. 0 B. null
C. Compile Error D. NullPointerException at runtime

Answer and Explanation

Answer: NullPointerException at runtime

Explanation:
In the above code the array reference variable x has been declared but it has not been instantiated i.e. the new statement is missing, for example:
private static int[]x = new int[5];
private static int[x] declares a static i.e. class level array.
the "new" keyword is the word that actually creates said array.
int[5] in association with the new sets the size of the array. so since the above code contains no new or size decalarations when you try and access x[0] you are trying to access a member of an array that has been declared but not intialized hence you get a NullPointerException at runtime.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. Which class does not override the equals() and hashCode() methods, inheriting them directly from class Object?
A. java.lang.String B. java.lang.Double
C. java.lang.StringBuffer D. java.lang.Character

Answer and Explanation

Answer: java.lang.StringBuffer

Explanation:
java.lang.StringBuffer is the only class in the list that uses the default methods provided by class Object. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. Which interface does java.util.Hashtable implement?
A. Java.util.Map B. Java.util.List
C. Java.util.HashTable D. Java.util.Collection

Answer and Explanation

Answer: Java.util.Map

Explanation:
Hash table based implementation of the Map interface.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What two statements are true about properly overridden hashCode() andequals() methods?

1. hashCode() doesn't have to be overridden if equals() is.
2. equals() doesn't have to be overridden if hashCode() is.
3. hashCode() can always return the same value, regardless of the object that invoked it.
4. equals() can be true even if it's comparing different objects.
A. 1 and 2 B. 2 and 3
C. 3 and 4 D. 1 and 3

Answer and Explanation

Answer: 3 and 4

Explanation:
(3) and (4) are correct.
(1) and (2) are incorrect because by contract hashCode() and equals() can't be overridden unless both are overridden.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. Which statement is true for the class java.util.ArrayList?
A. The elements in the collection are ordered. B. The collection is guaranteed to be immutable.
C. The elements in the collection are guaranteed to be unique. D. The elements in the collection are accessed using a unique key.

Answer and Explanation

Answer: The elements in the collection are ordered.

Explanation:
Yes, always the elements in the collection are ordered.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class W{
Public static void main(String args[]){
Int i=10;
Method(i); }
Static void method(Object o){
System.out.println(“object”); }
Static void method(Number n){
System.out.println(“number”); }
}
A. compile time error B. runtime error
C. object D. number

Answer and Explanation

Answer: number

Explanation:
from main() method calling Method by passing 10 as parameter. For int values Number is a super class , so output is option d).. Object is a super class of all the different type of class not for primitives.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. class W{
Static void method(int I,int j ){
System.out.println(“int”); }
Static void method(byte…b){
System.out.println(“byte”); }

Public static void main(String args[]){
byte i=10;
method(i,i); }
}
A. compile time error B. runtime error
C. int D. byte

Answer and Explanation

Answer: int

Explanation:
while calling method() from main() we are passing exactly two parameter as byte , as we are calling by passing only two so it will display int.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. class W{
Static void method(Byte I,Byte j ){
System.out.println(“BByte”); }
Static void method(byte…b){
System.out.println(“byte”); }

Public static void main(String args[]){
byte i=10;
method(i,i); }
}
A. compile time error B. runtime error
C. int D. byte

Answer and Explanation

Answer: int

Explanation:
while calling method() from main() we are passing exactly two parameter as byte , as we are calling by passing only byte that to as primitive type it display output as byte.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class W{
Static void method(Byte I,Byte I ){
System.out.println(“BByte”); }
Static void method(byte…b){
System.out.println(“byte”); }

Public static void main(String args[]){
byte i=10;
method(i,i); }
}
A. compile time error B. runtime error
C. int D. byte

Answer and Explanation

Answer: compile time error

Explanation:
while calling method() from main() we are passing exactly two parameter as byte , as we are calling by passing only byte that to as primitive type but in a arguments both variable name is same that is I.. so it will throw compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
11. Class A{
Class B{
Int I;
}

public static void main(String args[]){
B b1=new B();
System.out.println(b1.i);
}
}
A. compile time error B. runtime error
C. 0 D. null

Answer and Explanation

Answer: compile time error

Explanation:
There is no explanation...

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.